<template>
	<el-dialog :title="dataForm.id ? '修改' : '新增'" :close-on-click-modal="false" :visible.sync="visible">
		<el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">

			<el-form-item label="门店名称" prop="name">
				<el-input v-model="dataForm.name" placeholder="门店名称" />
			</el-form-item>
			<el-form-item label="门店地址" prop="address">
				<el-input v-model="dataForm.address" placeholder="门店地址" />
			</el-form-item>
			<el-form-item label="门店级别" prop="grade">
				<el-select v-model="dataForm.grade" placeholder="请选择">
					<el-option v-for="item in gradeOptions" :key="item.value" :label="item.label" :value="item.value">
					</el-option>
				</el-select>
			</el-form-item>

		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</span>
	</el-dialog>
</template>

<script>
	import {
		info,
		add,
		update
	} from '@/api/sys/dept'

	export default {
		data() {
			return {
				visible: false,
				dataTree: [],
				dataForm: {
					id: 0,
					name: '',
					grade: null,
					address: ''
				},
				deptoptions: [],
				dataRule: {
					address: [{
						required: true,
						message: '地址不能为空',
						trigger: 'blur'
					}],
					name: [{
						required: true,
						message: '门店名称不能为空',
						trigger: 'blur'
					}],
					sort: [{
						required: true,
						message: '排序不能为空',
						trigger: 'blur'
					}]
				},
				gradeOptions: [{
				          value: 0,
				          label: '门店'
				        }, {
				          value: 1,
				          label: '总店'
				        }]
			}
		},
		methods: {
			init(id) {
				this.dataForm.id = id || '';
				this.visible = true;
				this.$nextTick(() => {
					this.$refs['dataForm'].resetFields();
					if (id) {
						this.request(info, id, this, data => {
							this.dataForm = data
							console.log(data)
							// this.getTree(id)
						})
					} else {
						// this.getTree()
					}

				});
			},
			submitHandle() {
				let promiseFun;
				if (this.dataForm.id) {
					promiseFun = update;
				} else {
					promiseFun = add;
				}
				this.handleSubmit(promiseFun, this.$refs["dataForm"], this, data => {
					this.visible = false;
					this.$emit('refreshDataList');
				});
			},
			// getTree(id){
			//   this.request(treeList, id?id:-1, this, data => {
			//       if (this.dataForm.id !== "") {
			//           for (let i = 0;i < data.length;i++){
			//               if (data[i].id === this.dataForm.id) {
			//                   data.splice(i,1);
			//               }
			//           }

			//       }
			//     this.dataTree = data
			//   } )
			// },

		}
	}
</script>
